﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using WpfApp1.Models;
using WpfApp1.Servcies;

namespace WpfApp1
{
    /// <summary>
    /// TreeViewDemo5.xaml 的交互逻辑
    /// 参考：https://www.cnblogs.com/AtTheMoment/p/15466932.html
    /// 有时间做下这里面的功能：https://www.cnblogs.com/wuhuacong/p/17711601.html
    /// </summary>
    public partial class TreeViewDemo5 : Window
    {
        List<Country> _countries;
        List<Brand> _brands;
        List<Car> _cars;

        public TreeViewDemo5()
        {
            InitializeComponent();

            _countries = CountryService.All();
            _brands = BrandService.All();
            _cars = CarService.All();

            treeView1.ItemsSource = GetTree();
        }

        public List<Country> GetTree()
        {
            foreach (var brand in _brands)
            {
                brand.Cars = _cars.FindAll(m => m.BrandId == brand.Id);
            }

            foreach (var country in _countries)
            {
                country.Brands = _brands.FindAll(m => m.CountryId == country.Id);
            }

            return _countries;
        }
    }
}
